%ifndef PRINT_TYPE_ASM
%define PRINT_TYPE_ASM

%include "syscalls.asm"

section .data
tag_bool	db	"<bool....>"
tag_nil		db	"<nil.....>"
tag_symbol	db	"<symbol..>"
tag_char	db	"<char....>"
tag_number	db	"<number..>"
tag_cons	db	"<cons....>"
tag_closure	db	"<closure.>"
tag_string	db	"<string..>"
tag_vecrec	db	"<record..>"
tag_ioport	db	"<io-port.>"
tag_gcredir	db	"<gc-redir>"

section .text

print_type:
	;; INPUT: scheme object in rax
	;; SIDE: prints out its type
	;; OUTPUT: leaves rax unchanged
	
	mov	rcx, rax
	and	rcx, 0b111
	
	cmp	rcx, 0b000
	je	.bool_nil_symbol_char
	
	cmp	rcx, 0b001
	jne	.skip1
	mov	rsi, tag_number
	jmp	.p
.skip1:

	cmp	rcx, 0b010
	jne	.skip2
	mov	rsi, tag_cons
	jmp	.p
.skip2:

	cmp	rcx, 0b011
	jne	.skip3
	mov	rsi, tag_closure
	jmp	.p
.skip3:

	cmp	rcx, 0b100
	jne	.skip4
	mov	rsi, tag_string
	jmp	.p
.skip4:

	cmp	rcx, 0b101
	jne	.skip5
	mov	rsi, tag_vecrec
	jmp	.p
.skip5:

	cmp	rcx, 0b110
	jne	.skip6
	mov	rsi, tag_ioport
	jmp	.p
.skip6:

	mov	rsi, tag_gcredir

.p:
	mov	rax, sys_write
	mov	rdi, 0
	mov	rdx, 10
	syscall
	ret

.bool_nil_symbol_char:
	mov     rcx, rax
	and	rcx, 0b11000
	
	cmp	rcx, 0b00000
	jne	.skip7
	mov	rsi, tag_bool
	jmp	.p
.skip7:

	cmp	rcx, 0b01000
	jne	.skip8
	mov	rsi, tag_nil
	jmp	.p
.skip8:

	cmp	rcx, 0b10000
	jne	.skip9
	mov	rsi, tag_symbol
	jmp	.p
.skip9:

	;; 0b11000
	mov	rsi, tag_char
	jmp	.p

%endif
